// pages/address/addAddress/addAddress.js
const app = getApp()
var service = require('../../../util/service')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recId:"",
    recManName:"",
    tel:"",
    attribute1:"",
    detailAddress:"",
    attribute2:"",
    attribute3:""
  },
  formSubmit: function (e) {
    let { recManName, tel, attribute1, detailAddress, attribute2, attribute3 } = e.detail.value;
    if (!recManName || !tel || !detailAddress || !attribute2) {
      wx.showToast({
        title: "请完善信息再保存",
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return;
    }
    var user = app.userInfo;
    var address = e.detail.value;
    address.userId = user.buyId;
    address.recId = this.data.recId;
    if (address.recId){
      service.AJAX(`smc/base/SystemReceiveAddress/updSystemReceiveAddress.do`, address, res => {
        wx.showToast({
          title: "保存成功",
          duration: 1000,
          mask: true
        })
        wx.navigateBack();
      }, err => { }, "POST");
    }else{
      service.AJAX(`smc/base/SystemReceiveAddress/addSystemReceiveAddress.do`, address, res => {
        wx.showToast({
          title: "保存成功",
          duration: 1000,
          mask: true
        })
        wx.navigateBack();
      }, err => { }, "POST");
    }
    
  },
  getAddress:function(){
    var that = this;
    wx.chooseLocation({
      success: function (res2) {
        that.setData({
          detailAddress: res2.address
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    if(id){
      service.AJAX(`smc/base/SystemReceiveAddress/findSystemReceiveAddress.json`, { "id":id }, res => {
        that.setData({ recId:id, recManName: res.recManName, attribute1: res.attribute1, tel: res.tel, detailAddress: res.detailAddress, attribute2: res.attribute2, attribute3: res.attribute3 });
      }, err => { }, "POST");
    }
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})